package com.example.soft.demos.web;

import java.util.ArrayList;
import java.util.List;

public class Client {
    public static void main(String[] args) {
        // 创建论文列表
        List<Paper> papers = new ArrayList<>();
        papers.add(new Thesis("软件构造原理"));
        papers.add(new JournalPaper("设计模式实践"));
        papers.add(new ConferencePaper("敏捷开发指南"));
        papers.add(new JournalPaper("测试技术研究"));

        // 使用随机选择策略
        PaperList paperList = new PaperList("刘钢老师", papers, new RandomSelectionStrategy());

        // 创建学生
        List<Student> students = new ArrayList<>();
        students.add(new Student("张三", 1, PaperType.Thesis));
        students.add(new Student("李四", 2, PaperType.JournalPaper));
        students.add(new Student("王五", 1, PaperType.ConferencePaper));

        // 模拟多线程选择论文
        for (Student student : students) {
            new Thread(() -> {
                try {
                    List<Paper> selectedPapers = paperList.selectPaperByRandom(student);
                    System.out.println(Thread.currentThread().getName() + " 选择结果：");
                    for (Paper p : selectedPapers) {
                        System.out.println("- " + p.getTitle());
                    }
                } catch (NoEnoughPapersException e) {
                    System.err.println(e.getMessage());
                }
            }).start();

            // 模拟时间间隔
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // 输出最终选择结果
        try {
            Thread.sleep(2000); // 等待所有线程完成
            System.out.println("\n=== 最终论文选择结果 ===");
            System.out.println(paperList.getSelectionResult());

            // 访问者模式应用：统计选课学生数量
            PaperSelectionVisitor visitor = new CountStudentsVisitor();
            paperList.getSelectionResult().accept(visitor);
            System.out.println("选课学生总数：" + ((CountStudentsVisitor) visitor).getStudentCount() + " 人");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}